package org.jeecg.modules.pucheng.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.pucheng.entity.CombinedUser;
import org.jeecg.modules.pucheng.service.ICombinedUserService;
import org.jeecg.modules.pucheng.vo.UserVo;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.HttpCookie;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 综合用户信息
 * @Author: jeecg-boot
 * @Date: 2019-11-01
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "综合用户信息")
@RestController
@RequestMapping("/pucheng/combinedUser")
public class CombinedUserController extends JeecgController<CombinedUser, ICombinedUserService> {
    @Autowired
    private ICombinedUserService combinedUserService;
    @Autowired
    private LuZhangController luZhangController;
    @Autowired
    private HeZhangController heZhangController;
    @Autowired
    private ISysUserService userService;

    @ApiOperation("刷新综合Token")
    @RequestMapping(value = "/flushToken", method = RequestMethod.GET)
    public Result<Object> flushToken() {
        //Result<Object> resultLz = luZhangController.loginSystem();
        Result<Object> resultHz = heZhangController.loginSystem();
        Result<Object> resultLz = luZhangController.loginSystem();
        if (resultHz.getCode() == 200 && resultLz.getCode() == 200) {
            return Result.ok("刷新成功");
        }
        return Result.error("刷新失败");
    }


    private Result<Object> loginSystem1(String username, String password) {
        String url = "http://npszhhz.cn/RiverChiefSystem/loginController/login.do";
        Map<String, Object> param = new HashMap<>();
        param.put("username", username);
        param.put("password", password);
        param.put("type", "web");
        HttpResponse response = HttpRequest.post(url).header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded").form(param).execute();
        String resultStr = response.body();
        List<HttpCookie> cookieList = response.getCookies();
        Map<String, String> cookies = cookieList.stream().collect(Collectors.toMap(HttpCookie::getName, HttpCookie::getValue));
        JSONObject jsonObject = JSONObject.fromObject(resultStr);
        // {"result":1,"permissions":["videosurveillance:view","examinestatistical:view","onemap:view","home:view","usermanage:view","workmanage:view","organizationinformation:view"],"roles":["河长办"],"message":"登录成功","user":{"userId":3798,"userName":"nppcxoffice","password":null,"realName":"浦城县河长办","createTime":null,"modifiedTime":null,"lastTime":1572580765000,"email":"","phone":"","regionId":"350722000000","region":{"regionId":"350722000000","regionName":"浦城县","parentId":null,"isParent":0,"level":0,"levelName":null,"longitute":null,"latitude":null,"remark":null},"regionName":"浦城县","riverList":[{"riverId":null,"riverName":null,"riverLevel":null,"isParent":null,"parentId":null,"parentName":null,"fId":null,"regionId":"350722000000","userId":3798,"remark":null,"levelName":null,"regionName":null,"userList":null,"type":0,"remoteRiverId":null,"fid":null}],"orgionRole":"","orgionOrganization":"","token":"2581B9A5F231DBB6BE459B6B2A2BF1C5","organizationId":52,"organizationName":"浦城县河长办","roleId":4,"roleName":"浦城县河长办","roleShortName":"河长办","roleType":"40","status":1,"remark":null,"remoteUserId":null,"min":0,"pageSize":0}}
        log.info(jsonObject.toString());
        String user = jsonObject.getString("user");
//        tokens.put("nppcxoffice",token);
        cn.hutool.json.JSONObject json = new cn.hutool.json.JSONObject(user);
        UserVo vo = JSONUtil.toBean(json, UserVo.class);
        if (ObjectUtil.isNotEmpty(vo)) {
            //tokens.put("nppcxoffice",vo.getToken());
            cookies.put("organizationId", String.valueOf(vo.getOrganizationId()));
            cookies.put("roleName", vo.getRealName());
            cookies.put("regionId", vo.getRegionId());
            cookies.put("roleId", String.valueOf(vo.getRoleId()));
            cookies.put("roleName", String.valueOf(vo.getRoleName()));
            cookies.put("roleType", String.valueOf(vo.getRoleType()));
            cookies.put("userId", String.valueOf(vo.getUserId()));
            cookies.put("userName", vo.getUserName());
            cookies.put("token", vo.getToken());
            StringBuffer sql = new StringBuffer();
            for (Map.Entry<String, String> entry : cookies.entrySet()) {
                String mapKey = entry.getKey();
                String mapValue = entry.getValue();
                sql.append(mapKey + "=" + mapValue + ";");
            }
            String cookie = sql.toString().substring(0, sql.toString().length() - 1);
            return Result.ok(cookies);
        }
        return Result.error("请求河长制服务器失败");
    }

    private Result<Object> loginSystem2(String username, String password) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return Result.error("请设置路长用户名或密码");
        }
        String authCode = "";
        String codeId = "";
        String codeurl = "https://fjjtj.xmheshu.cn/admin/authCode?authKey=null&sessionId=null";
        Map<String, String> header = new HashMap<>();
        header.put("Accept", "application/json, text/plain, */*");
        header.put("Accept-Encoding", "gzip, deflate, br");
        header.put("Accept-Language", "zh-CN,zh;q=0.9");
        header.put("Connection", "keep-alive");
        //header.put("Content-Length","0");
        header.put("Content-Type", "application/json;charset=UTF-8");
        header.put("Host", "fjjtj.xmheshu.cn");
        header.put("Origin", "http://www.shncl.cn");
        header.put("Referer", "http://www.shncl.cn/");
        header.put("Sec-Fetch-Mode", "cors");
        header.put("Sec-Fetch-Site", "cross-site");
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36");
        HttpResponse response1 = HttpRequest.post(codeurl).header(Header.CONTENT_TYPE, "application/json;charset=UTF-8").addHeaders(header).execute();
        String resultStr1 = response1.body();
        //{"code":200,"desc":"成功","data":{"id":null,"areaid":null,"pid":null,"account":null,"password":null,"displayName":null,"perfScore":null,"qrcodeId":null,"email":null,"userAuthType":null,"mobile":null,"address":null,"company":null,"isLogined":null,"mobileType":null,"directCounty":null,"createTime":null,"qq":null,"towmid":null,"villageid":null,"groupSort":null,"facePicture":null,"isOpenFace":null,"isEnableFace":null,"xmaintPatro":null,"maintId":null,"indexNo":null,"job":null,"scodeno":null,"enabled":null,"codeId":"admin_login_auth_custom_dc555488539f46e4bcfff1decd1d9182","authCode":"r7uc","clientType":null}}
        JSONObject jsonObject1 = JSONObject.fromObject(resultStr1);
        try {
            JSONObject json = jsonObject1.getJSONObject("data");
            codeId = json.getString("codeId");
            authCode = json.getString("authCode");
        } catch (JSONException e) {
            return Result.error("获取验证码失败");
        }
        String url = "https://fjjtj.xmheshu.cn/admin/login?authKey=null&sessionId=null";
        Map<String, Object> param = new HashMap<>();
        param.put("account", username);
        param.put("password", password);
        param.put("authCode", authCode);
        param.put("codeId", codeId);
        HttpResponse response = HttpRequest.post(url).header(Header.CONTENT_TYPE, "application/json;charset=UTF-8").addHeaders(header).body(JSONUtil.parseFromMap(param)).execute();
        String resultStr = response.body();
        //{"code":200,"desc":"登录成功","data":{"authKey":"MUY4QzEzQzBGQjY1MDdFRjVFRERFN0NCNDgxMzQ4NEMxQzhFRDRGNTQzRTRDODBFNzRGRTg3NDdBRjEyREJDOTkzODNBRkUxRDgwNEVEQjM=","areaid":350722,"userAuthType":7,"areaName":"浦城县","displayName":"路长办","sessionId":"09257D86E50C32639902267FD90ECC2A","userId":1000001511,"account":"350722","menuTree":[{"id":5002750,"areaid":350722,"menuNo":3602000,"applicationCode":null,"menuParentNo":1,"menuOrder":1,"menuName":"全县总览","menuUrl":"","menuType":1,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":null,"parentName":null,"child":[{"id":5002751,"areaid":350722,"menuNo":3602001,"applicationCode":null,"menuParentNo":3602000,"menuOrder":1,"menuName":"农村公路地图总览","menuUrl":"/file/overview","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":null,"parentName":null,"child":null}]},{"id":5002755,"areaid":350722,"menuNo":3603000,"applicationCode":null,"menuParentNo":1,"menuOrder":2,"menuName":"公路档案","menuUrl":"","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":null,"parentName":null,"child":[{"id":5002756,"areaid":350722,"menuNo":3603001,"applicationCode":null,"menuParentNo":3603000,"menuOrder":1,"menuName":"花名册档案","menuUrl":"/user/patrol","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535291821000,"parentName":null,"child":null},{"id":5002757,"areaid":350722,"menuNo":3603002,"applicationCode":null,"menuParentNo":3603000,"menuOrder":2,"menuName":"公路路线档案","menuUrl":"/file/path","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535291852000,"parentName":null,"child":null}]},{"id":5002767,"areaid":350722,"menuNo":3604000,"applicationCode":null,"menuParentNo":1,"menuOrder":3,"menuName":"事务审批","menuUrl":"","menuType":1,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292167000,"parentName":null,"child":[{"id":5002768,"areaid":350722,"menuNo":3604001,"applicationCode":null,"menuParentNo":3604000,"menuOrder":1,"menuName":"灾毁保险","menuUrl":"/transaction/insuranceReport","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292196000,"parentName":null,"child":null},{"id":5002769,"areaid":350722,"menuNo":3604002,"applicationCode":null,"menuParentNo":3604000,"menuOrder":2,"menuName":"公众上报管理","menuUrl":"/transaction/toTaskNews","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292221000,"parentName":null,"child":null},{"id":5002770,"areaid":350722,"menuNo":3604003,"applicationCode":null,"menuParentNo":3604000,"menuOrder":3,"menuName":"我的事件","menuUrl":"/transaction/toMyEvent","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292245000,"parentName":null,"child":null}]},{"id":5002774,"areaid":350722,"menuNo":3605000,"applicationCode":null,"menuParentNo":1,"menuOrder":4,"menuName":"农路养护记录","menuUrl":"","menuType":1,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292396000,"parentName":null,"child":[{"id":5002775,"areaid":350722,"menuNo":3605001,"applicationCode":null,"menuParentNo":3605000,"menuOrder":1,"menuName":"巡查记录","menuUrl":"/statistics/roadInspect","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292443000,"parentName":null,"child":null},{"id":5002776,"areaid":350722,"menuNo":3605002,"applicationCode":null,"menuParentNo":3605000,"menuOrder":2,"menuName":"上报事件分布","menuUrl":"/statistics/mapEvent","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292465000,"parentName":null,"child":null},{"id":5002777,"areaid":350722,"menuNo":3605003,"applicationCode":null,"menuParentNo":3605000,"menuOrder":3,"menuName":"专管员事件记录","menuUrl":"/county/allEvent","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292490000,"parentName":null,"child":null},{"id":5002778,"areaid":350722,"menuNo":3605004,"applicationCode":null,"menuParentNo":3605000,"menuOrder":4,"menuName":"公众事件记录","menuUrl":"/county/allPubliceEvent","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292656000,"parentName":null,"child":null}]},{"id":5002782,"areaid":350722,"menuNo":3607000,"applicationCode":null,"menuParentNo":1,"menuOrder":6,"menuName":"灾毁保险","menuUrl":"","menuType":1,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292822000,"parentName":null,"child":[{"id":5002783,"areaid":350722,"menuNo":3607001,"applicationCode":null,"menuParentNo":3607000,"menuOrder":1,"menuName":"灾毁事件","menuUrl":"/insurance/report","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292849000,"parentName":null,"child":null},{"id":5002784,"areaid":350722,"menuNo":3607002,"applicationCode":null,"menuParentNo":3607000,"menuOrder":2,"menuName":"灾毁事件管理","menuUrl":"/insurance/daiban","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292886000,"parentName":null,"child":null}]},{"id":5002785,"areaid":350722,"menuNo":3608000,"applicationCode":null,"menuParentNo":1,"menuOrder":7,"menuName":"查询统计","menuUrl":"","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292914000,"parentName":null,"child":[{"id":5002786,"areaid":350722,"menuNo":3608001,"applicationCode":null,"menuParentNo":3608000,"menuOrder":1,"menuName":"专管员统计","menuUrl":"/statistics/patrolman","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535292968000,"parentName":null,"child":null}]},{"id":5002799,"areaid":350722,"menuNo":3611000,"applicationCode":null,"menuParentNo":1,"menuOrder":10,"menuName":"专管员考核","menuUrl":"","menuType":1,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535293381000,"parentName":null,"child":[{"id":5002800,"areaid":350722,"menuNo":3611001,"applicationCode":null,"menuParentNo":3611000,"menuOrder":1,"menuName":"考核标准","menuUrl":"/perf/rule","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535293408000,"parentName":null,"child":null},{"id":5002801,"areaid":350722,"menuNo":3611002,"applicationCode":null,"menuParentNo":3611000,"menuOrder":2,"menuName":"考核记录","menuUrl":"/perf/record","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535293430000,"parentName":null,"child":null},{"id":5002802,"areaid":350722,"menuNo":3611003,"applicationCode":null,"menuParentNo":3611000,"menuOrder":3,"menuName":"考核成绩","menuUrl":"/perf/history","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1535293513000,"parentName":null,"child":null}]},{"id":5002803,"areaid":350722,"menuNo":3612000,"applicationCode":null,"menuParentNo":1,"menuOrder":11,"menuName":"消息管理","menuUrl":"","menuType":1,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1520904196000,"parentName":null,"child":[{"id":5002805,"areaid":350722,"menuNo":3612003,"applicationCode":null,"menuParentNo":3612000,"menuOrder":null,"menuName":"banner管理","menuUrl":"/message/banner","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1522320016000,"parentName":null,"child":null},{"id":5002807,"areaid":350722,"menuNo":3612005,"applicationCode":null,"menuParentNo":3612000,"menuOrder":null,"menuName":"法律法规管理","menuUrl":"/message/law","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1523507586000,"parentName":null,"child":null},{"id":5002804,"areaid":350722,"menuNo":3612001,"applicationCode":null,"menuParentNo":3612000,"menuOrder":null,"menuName":"新闻管理","menuUrl":"/message/new","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1520904260000,"parentName":null,"child":null},{"id":5002806,"areaid":350722,"menuNo":3612004,"applicationCode":null,"menuParentNo":3612000,"menuOrder":null,"menuName":"公告管理","menuUrl":"/message/notice","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1523507083000,"parentName":null,"child":null},{"id":5002808,"areaid":350722,"menuNo":3612006,"applicationCode":null,"menuParentNo":3612000,"menuOrder":null,"menuName":"学习培训管理","menuUrl":"/message/train","menuType":2,"menuIcon":"","enabled":1,"isLeaf":1,"createTime":1523507641000,"parentName":null,"child":null}]}],"navTitle":"浦城县农村公路智慧路长信息管理平台"}}
        JSONObject jsonObject = JSONObject.fromObject(resultStr);
        if (jsonObject.getInt("code") == 200) {
            JSONObject json = jsonObject.getJSONObject("data");
            String authKey = json.getString("authKey");
            String sessionId = json.getString("sessionId");
            return Result.ok("authKey=" + authKey + "&sessionId=" + sessionId);
        } else {
            return Result.error("路长账号配置有误,请修改");
        }
    }

    @ApiOperation("跳转平台登录检验 1 河长  2 路长")
    @RequestMapping(value = "/checkBeforeLogin", method = RequestMethod.GET)
    public Result<Object> checkBeforeLogin(@ApiParam(required = true, name = "type", value = "1 河长  2 路长") @RequestParam int type) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LambdaQueryWrapper<CombinedUser> querySaWrapper = new LambdaQueryWrapper<CombinedUser>();
        querySaWrapper.eq(CombinedUser::getUserId, sysUser.getId());
        List<CombinedUser> users = combinedUserService.list(querySaWrapper);
        if (users.size() > 1) {
            return Result.error("数据有误");
        } else if (users.size() == 0) {
            return Result.error("账号未配置");
        }
        CombinedUser login = users.get(0);
        switch (type) {
            case 1:
                //Result<Object> result1 = loginSystem1(login.getUsername1(), login.getPassword1());  //河长登录
                Result<Object> result1 = heZhangController.loginSystem();
                if (result1.getCode() == 200) {
                    Map<String, String> map = new HashMap<>();
                    map.put("username", login.getUsername1());
                    map.put("password", login.getPassword1());
                    return Result.ok(map);
                }
                break;
            case 2:
                //Result<Object> result2 = loginSystem2(login.getUsername2(), login.getPassword2()); //路长登录
                Result<Object> result2 = luZhangController.loginSystem();
                if (result2.getCode() == 200) {
                    Map<String, String> map = new HashMap<>();
                    map.put("username", login.getUsername2());
                    map.put("password", login.getPassword2());
                    return Result.ok(map);
                }
                break;
            default:
                break;
        }
        return Result.error("错误的登录类型");
    }


    /**
     * 分页列表查询
     *
     * @param combinedUser
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "综合用户信息-分页列表查询")
    @ApiOperation(value = "综合用户信息-分页列表查询", notes = "综合用户信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CombinedUser combinedUser,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<CombinedUser> queryWrapper = QueryGenerator.initQueryWrapper(combinedUser, req.getParameterMap());
        Page<CombinedUser> page = new Page<CombinedUser>(pageNo, pageSize);
        IPage<CombinedUser> pageList = combinedUserService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @ApiOperation("根据当前账号获取三平台账号配置")
    @RequestMapping(value = "/getListByCurrent", method = RequestMethod.GET)
    public Result<?> getListByCurrent() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LambdaQueryWrapper<CombinedUser> querySaWrapper = new LambdaQueryWrapper<CombinedUser>();
        querySaWrapper.eq(CombinedUser::getUserId, sysUser.getId());
        List<CombinedUser> users = combinedUserService.list(querySaWrapper);
        if (users.size() > 1) {
            return Result.error("数据有误");
        } else if (users.size() == 0) {
            return Result.error("账号未配置");
        }
        CombinedUser login = users.get(0);
        return Result.ok(login);
    }

    @ApiOperation("根据当前账号添加/修改三平台账号配置")
    @RequestMapping(value = "/editByCurrent", method = RequestMethod.POST)
    public Result<?> editByCurrent(@RequestBody CombinedUser combinedUser) {
        if (oConvertUtils.isEmpty(combinedUser)) {
            return Result.error("没有修改参数");
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LambdaQueryWrapper<CombinedUser> querySaWrapper = new LambdaQueryWrapper<CombinedUser>();
        querySaWrapper.eq(CombinedUser::getUserId, sysUser.getId());
        List<CombinedUser> users = combinedUserService.list(querySaWrapper);
        if (users.size() > 1) {
            return Result.error("数据有误");
        } else if (users.size() == 0) {
            CombinedUser login = new CombinedUser();
            login.setId(UUIDGenerator.generate());
            login.setName(sysUser.getUsername());
            login.setUserId(sysUser.getId());
            if (StringUtils.isNotBlank(combinedUser.getUsername1())) {
                login.setUsername1(combinedUser.getUsername1());
                login.setPassword1(combinedUser.getPassword1());
            }
            if (StringUtils.isNotBlank(combinedUser.getUsername2())) {
                login.setUsername2(combinedUser.getUsername2());
                login.setPassword2(combinedUser.getPassword2());
            }
            if (StringUtils.isNotBlank(combinedUser.getUsername3())) {
                login.setUsername3(combinedUser.getUsername3());
                login.setPassword3(combinedUser.getPassword3());
            }
            combinedUserService.save(login);
            return Result.ok(login);
        } else {
            CombinedUser login = users.get(0);
            if (StringUtils.isNotBlank(combinedUser.getUsername1())) {
                login.setUsername1(combinedUser.getUsername1());
                login.setPassword1(combinedUser.getPassword1());
            }
            if (StringUtils.isNotBlank(combinedUser.getUsername2())) {
                login.setUsername2(combinedUser.getUsername2());
                login.setPassword2(combinedUser.getPassword2());
            }
            if (StringUtils.isNotBlank(combinedUser.getUsername3())) {
                login.setUsername3(combinedUser.getUsername3());
                login.setPassword3(combinedUser.getPassword3());
            }
            combinedUserService.updateById(login);
            return Result.ok(login);
        }
    }


    /**
     * 添加
     *
     * @param combinedUser
     * @return
     */
    @AutoLog(value = "综合用户信息-添加")
    @ApiOperation(value = "综合用户信息-添加", notes = "综合用户信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CombinedUser combinedUser) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (StringUtils.isBlank(sysUser.getId())) {
            SysUser user = userService.getUserByName(sysUser.getUsername());
            combinedUser.setUserId(user.getId());
        } else {
            combinedUser.setUserId(sysUser.getId());
        }
        combinedUserService.save(combinedUser);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param combinedUser
     * @return
     */
    @AutoLog(value = "综合用户信息-编辑")
    @ApiOperation(value = "综合用户信息-编辑", notes = "综合用户信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CombinedUser combinedUser) {
        combinedUserService.updateById(combinedUser);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "综合用户信息-通过id删除")
    @ApiOperation(value = "综合用户信息-通过id删除", notes = "综合用户信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        combinedUserService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "综合用户信息-批量删除")
    @ApiOperation(value = "综合用户信息-批量删除", notes = "综合用户信息-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.combinedUserService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "综合用户信息-通过id查询")
    @ApiOperation(value = "综合用户信息-通过id查询", notes = "综合用户信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CombinedUser combinedUser = combinedUserService.getById(id);
        return Result.ok(combinedUser);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param combinedUser
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CombinedUser combinedUser) {
        return super.exportXls(request, combinedUser, CombinedUser.class, "综合用户信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CombinedUser.class);
    }

}
